package dataswitch

import (
	"errors"
	"fmt"
	"github.com/shopspring/decimal"
)

//Yuan2Fen 元转分,乘以100后，保留整数部分
func Yuan2Fen(price string) (int64, error) {
	d1 := decimal.New(1, 2)
	d2 := decimal.New(1, 0)
	amountFloat, err := decimal.NewFromString(price)
	if err != nil {
		return 0, errors.New("转换数据错误")
	}
	return amountFloat.Mul(d1).DivRound(d2, 0).IntPart(), nil
}

//Fen2Yuan 分转元
func Fen2Yuan(price int64) string {
	return fmt.Sprintf("%.2f", float64(price)/100.0)
}

//Fen2YuanWithFloat 分专元float
func Fen2YuanWithFloat(price int64) float64 {
	d := decimal.New(1, 2)
	result := decimal.NewFromInt(price).DivRound(d, 2).InexactFloat64()
	return result
}
